#include <bits/stdc++.h>
#include <windows.h>
#include <unistd.h>
#include <stdlib.h>
#include "../../include/json.hpp"

using namespace std;
using json = nlohmann::json;

std::map<std::string, int> Status = {
    {"say", 0},
    {"choice", 1},
    {"typing", 2},
    {"end", 3}
};

namespace process
{
    void Process(json jsonObject)
    {
        boolean isJump = false;
        json obj = jsonObject;
        std::string State;
        std::string msg;
        int i=0;
        for (i; i < obj.size(); i++)
        {
            auto &el = obj[i];
            if (isJump){
                el = obj[i];
                std::cout << el << endl;
                i--;
            }
            State = el["type"];
            switch (Status[State])
            {
                case 0:
                {
                    if (el["content"].is_array())
                    {
                        std::string arr[] = {el["content"]};
                        for (int k = 0; k < sizeof(arr) / sizeof(arr[0]); k++)
                        {
                            msg = arr[k];
                            std::cout << msg << '\n' << endl;
                        }
                    }
                    else
                    {
                        msg = el["content"];
                        std::cout << msg << endl;
                    }
                    break;
                }
                case 1:
                {
                    if (el["tips"].is_array()){
                        for (int i=0;i<el["tips"].size();i++){
                            msg = el["tips"][i];
                            std::cout << msg << endl;
                        }
                    }
                    std::string ans;
                    std::cin >> ans;
                    for (auto &choose : el["choice"])
                    {
                        std::cout << choose.contains(ans) <<endl;
                        if (choose.contains(ans)){
                            // string jumpTo = choose[ans]["jump"];
                            int jumpTo = choose[ans]["jump"];
                            std::cout << jumpTo << endl;
                            i = jumpTo;
                            i--;
                            isJump = true;
                            break;

                        }
                    }
                    break;
                }
                case 2:{
                    // int delay = el["delay"];
                    // std::cout << el["content"].size() << endl;
                    int delay = el["delay"];
                    if (el["content"].is_array()){
                        std::vector<std::string> content = el["content"];
                        // std::string[] content = {el["content"]};
                        for (int i=0;i<el["content"].size();i++){
                            msg = content.at(i);
                            for (int j=0;j<msg.length();j++){
                                std::cout << msg.at(j);
                                Sleep(delay);
                            }
                            std::cout << endl;
                        }
                    } else {
                        std::string content = el["content"];
                        msg = content;
                        if (delay == 0){
                            std::cout << msg << endl;
                            break;
                        }
                        int contentSize = content.length();
                        for (int k=0;k<contentSize;k++){
                            msg = content[k];
                            std::cout << msg;
                            Sleep(delay);
                        }
                        std::cout << endl;
                    }
                    break;
                }
                case 3:{
                    if (el["content"].is_array()){
                        for (auto &msg : el["content"]){
                            std::cout << msg << endl;
                        }
                    } else {
                        std::cout << el["content"] << endl;
                    }
                    exit(0);
                }
                default:{
                    break;
                }
            }
            if (isJump){
                continue;
            }
        }
    } // namespace process
}
